#!/usr/bin/env python

from mxit import handles
from mxit import encryption
from mxit import errors
import gettext

gettext.install('blendIT','locale');

class listener:

   def __init__(self,blend):
      self.blend = blend;

   def status_changed(self,status,reason):
      if status == handles.Status.DISCONNECTED:
         self.blend.currentlyConnected = False;

         if reason == handles.StatusChangeReason.TIMEOUT:
            self.blend.conn._close();
            self.blend.retryLogin();
         elif reason == handles.StatusChangeReason.NETWORK_ERROR:
            #print "Network Error";
            self.blend.conn._close();
            self.blend.gui.networkError();
         elif reason == handles.StatusChangeReason.REQUESTED:
            self.blend.gui.buddyWindow.clear();
         elif reason == handles.StatusChangeReason.AUTH_FAILED:
            r = self.blend.gui.requestPassword(_("Login failed. Please verify your password is correct by retyping it below\nor alternatively, re-create your BlendIt account"),True);
            #print 'lol', r;
            if r == -1:
               # Launch Account Creator
               self.blend.gui.destroyWindows();
               self.blend.gui.initiateFirstTimeWindow();
            elif r != False:
               # retry login
               self.blend.currentAccount.password = r;
               self.blend.login();
         else:
            self.blend.gui.disconnectedMessage(_("Could not login for an unknown reason."));
      elif status == handles.Status.ACTIVE:
         self.blend.currentlyConnected = True;
         self.blend.retrying = -1;
         
         self.blend.connected();
      #print "cL sc";


   def profile_updated(self,profile,gender=None,birthdate=None,
                       name=None,private=None):
      self.blend.currentAccount.gender = gender;
      self.blend.currentAccount.set_birthdate(birthdate);
      self.blend.currentAccount.name = str(name);
      self.blend.currentAccount.private = private;
      
